var CallRecordTable = {
    id: "CallRecordTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

CallRecordTable.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'flowNo', field: 'flowNo', visible: false, align: 'center', valign: 'middle'},
        {title: 'remarks', field: 'remarks', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'isexistfile', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'recFile', visible: false, align: 'center', valign: 'middle'},
        {
            title: '接通状态', field: 'result', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return '已接'
                } else if (value == 2) {
                    return '未接'
                } else if (value == 4 && $("#callStatus").val() == 2) {
                    return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'CallRecordTable.result4(" + '"' + row.remarks + '"' + ")' >已处理</span></a>"
                } else {
                    return '未接'
                }
            }
        },
        {
            title: '呼出号码', field: 'calleeId', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'CallRecordTable.recondPhone(" + '"' + row.flowNo + '","' + row.calleeId + '"' + ")' >" + value + "</span></a>"
            }
        },
        {
            title: '呼入号码', field: 'callerId', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'CallRecordTable.recondPhone(" + '"' + row.flowNo + '","' + row.callerId + '"' + ")' >" + value + "</span></a>"
            }
        },
        {
            title: '网咨姓名', field: 'name', valign: 'middle', align: 'center', sortable: true, width: 120,
        },
        {
            title: '呼入时间', field: 'beginTime', align: 'center', valign: 'middle', sortable: true, width: 110,
        },
        {
            title: '结束时间', field: 'endTime', align: 'center', valign: 'middle', sortable: true, width: 110,
        },
        {
            title: '时长', field: '', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return intervalTime(row.beginTime, row.endTime);
            }
        },
        {
            title: '跟进状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true, width: 90,
        },
        {
            title: '回访时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 90,
        },
        {
            title: '回访内容', field: 'revisitContent', align: 'center', valign: 'middle', sortable: true, width: 90,
        },
        {
            title: '录音记录', field: 'operation', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                if (row.isexistfile == 1) {
                    return '<button title="录音记录" onclick="CallRecordTable.play(' + "'" + row.id + "'" + ')"  style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-headphones"></span></button> '
                } else {
                    return "";
                }
            }
        },
        {
            title: '录音记录', field: 'operation1', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                if (row.isexistfile == 1) {
                    return '<button title="录音记录" onclick="CallRecordTable.play1(' + "'" + row.id + "'" + ')"  style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-headphones"></span></button> '
                } else {
                    return "";
                }
            }
        }
    ];
    return columns;
};

CallRecordTable.search = function () { // 查询预约客户列表
    CallRecordTable.table.refresh({query: CallRecordTable.formParams()});
};

CallRecordTable.close = function () { // 关闭窗口
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

CallRecordTable.result4 = function (remarks) {//点击表格展示详情
    alert(remarks);
};

CallRecordTable.formParams = function () { // 市场预约表单查询提交参数
    var queryData = {};
    queryData['areaAbbreviation'] = $("#areaAbbreviation").val();
    queryData['startCreateTime'] = $("#startCreateTime").val();  //建档时间
    queryData['endCreateTime'] = $("#endCreateTime").val();
    queryData['callStatus'] = $("#callStatus").val();
    queryData['wanName'] = $("#wanName").val();
    queryData['result'] = $("#result").val();
    return queryData;
};

CallRecordTable.getWangName = function () {
    var seleteNameValue = $('#userName').val();
    if (seleteNameValue != "") {
        $("#wanName option[value='" + seleteNameValue + "']").attr("selected", "selected");
    }
    CallRecordTable.search();
};

var parseParam = function (param) { // 把对象拼接成URL的键值对参数 param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

CallRecordTable.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CallRecordTable.seItem = selected[0];
        return true;
    }
};

CallRecordTable.play = function (id) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '播放录音',
        area: ['20%', '30%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/play/' + id
    });
    this.layerIndex = index;
};

CallRecordTable.play1 = function (id) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '播放录音',
        area: ['20%', '30%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/play1/' + id
    });
    this.layerIndex = index;
};
CallRecordTable.recondPhone = function (id, phone) {//点击表格展示详情.
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsCallRecord/checkRecond/' + id+ '/' + phone
    });
    this.layerIndex = index;
};

CallRecordTable.selectionTwo = function (text, obj) {
    $.ajax({
        url: Feng.ctxPath + "/reservationsCallRecord/getWanName",
        type: "post",
        data: {
            area: document.getElementById("areaAbbreviation").value
        },
        success: function (data) {
            var txt = '<option value="">请选择</option>';
            for (var i = 0; i < data.length; i++) {
                txt += ' <option value="' + data[i].account + '"> ' + ' &nbsp' + data[i].account + '</option>';
            }
            document.getElementById('wanName').innerHTML = txt;
            CallRecordTable.search();
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
};

function handleMissed() {
    var id = document.getElementById("id").value;
    var remarks = document.getElementById("remarks").value;
    $.ajax({
        url: Feng.ctxPath + "/reservationsCallRecord/handleMissed",
        type: "post",
        data: {
            id: id,
            remarks: remarks
        },
        success: function (data) {
            Feng.success('操作成功!');
            window.parent.CallRecordTable.search();
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
}

CallRecordTable.call = function (obj) {
    if (obj == "1") {
        $('#CallRecordTable').bootstrapTable('hideColumn', 'callerId');
        $('#CallRecordTable').bootstrapTable('showColumn', 'calleeId');
        $('#CallRecordTable').bootstrapTable('showColumn', 'operation');
        $('#CallRecordTable').bootstrapTable('hideColumn', 'operation1');
    } else {
        $('#CallRecordTable').bootstrapTable('showColumn', 'callerId');
        $('#CallRecordTable').bootstrapTable('hideColumn', 'calleeId');
        $('#CallRecordTable').bootstrapTable('showColumn', 'operation1');
        $('#CallRecordTable').bootstrapTable('hideColumn', 'operation');
    }
    $("#callStatus").val(obj);
    CallRecordTable.search();
};

function intervalTime(startTime, endTime) {
    //只需要时分秒
    var date1 = new Date(startTime);  //开始时间
    var date2 = new Date(endTime);    //结束时间
    var date3 = date2.getTime() - date1.getTime();  //时间差的毫秒数
    //计算小时
    var hours = Math.floor(date3 / (3600 * 1000));
    //计算小时后剩余的毫秒数
    var leave1 = date3 % (3600 * 1000);
    //计算相差分钟数
    var minutes = Math.floor(leave1 / (60 * 1000));
    //计算分钟数后剩余的毫秒数
    var leave2 = leave1 % (60 * 1000);
    //计算妙数
    var seconds = Math.round(leave2 / 1000);
    return hours + "小时 " + minutes + " 分钟" + seconds + " 秒";
}


CallRecordTable.resetSearch = function () { // 重置
    $("#marketInputPublic", parent.document).val("");
    $("#startFilingTime").val("");
    $("#endFilingTime").val("");
    $("#wanName").val("");
    $("#areaAbbreviation").val("");
    CallRecordTable.search();
};

CallRecordTable.hangup = function () { // 挂断
    $.ajax({
        url: parent.document.getElementById("whurl").value,
        type: "get",
        data: {
            "op": "onhook",
            "Exten": parent.document.getElementById("exten").value
        },
        success: function (data) {
            if (data == 100) {
                Feng.success('挂断成功!');
            } else if (data == 101) {
                Feng.success('分机号不存在!');
            } else if (data == 103) {
                Feng.success('参数错误!');
            } else if (data == 500) {
                Feng.success('其他错误!');
            }
        }
    })
};

CallRecordTable.to_handleMissed = function () { // 处理未接
    if (this.check()) {
        if (CallRecordTable.seItem.result == 2 && $("#callStatus").val() == 2) {
            var index = layer.open({
                type: 2,
                title: '未接电话处理',
                area: ['70%', '60%'],
                fix: true, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/reservationsCallRecord/to_handleMissed/' + CallRecordTable.seItem.flowNo + '/' + CallRecordTable.seItem.callerId+ '/' + CallRecordTable.seItem.id
            });
            this.layerIndex = index;
        }
    }
};

CallRecordTable.today = function () { // 点击上面状态筛选表格
    $("#startCreateTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endCreateTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    CallRecordTable.search();
};


$("#marketSearchAll", parent.document).click(function () { // 主页搜索
    CallRecordTable.search();
});

var scollPostion = 0;
CallRecordTable.getScrollPosition = function () { // 获取表格滚动到什么地方
    scollPostion = $('#CallRecordTable').bootstrapTable('getScrollPosition');
};

CallRecordTable.scrollTo = function () { // 指定表格滚动到什么地方
    $("#CallRecordTable").on('load-success.bs.table', function (data) {
        $('#CallRecordTable').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    var defaultColunms = CallRecordTable.initColumn();
    var table = new BSTable(CallRecordTable.id, "/reservationsCallRecord/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CallRecordTable.formParams());
    CallRecordTable.table = table.init();
    CallRecordTable.getWangName();
    CallRecordTable.today();
    CallRecordTable.call('1');
    menuNameHideColumn($("#menuName").val(), 'CallRecordTable');
    $("#callout").addClass('actionClick').siblings().removeClass('actionClick');
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {
        CallRecordTable.search();
    });
    checkDate = function () {
        CallRecordTable.search();
    };
    callbackTable = function () {
        CallRecordTable.search();
    };
});